package com.example.clear;

import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.app.usage.StorageStats;
import android.app.usage.StorageStatsManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.os.storage.StorageManager;
import android.util.Log;

import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;

public class ExternalUtil {

    /**
     * 获取缓存值
     */
    public static long getTotalCacheSize(Context context) {
        long externalCacheSize = 0L;
        long externalFileSize = 0L;
        long internalCacheSize = 0L;
        long internalFileSize = 0L;
        internalCacheSize = getFolderSize(context.getCacheDir());
//        internalFileSize  = getFolderSize(context.getFilesDir());
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            externalCacheSize = getFolderSize(context.getExternalCacheDir());
//            externalFileSize  = getFolderSize(context.getExternalFilesDir(""));
        }
        return internalCacheSize + externalCacheSize + internalFileSize + externalFileSize;
    }

    /**
     * 获取文件
     */
    public static long getFolderSize(File file) {
        long size = 0;
        if (file != null) {
            File[] fileList = file.listFiles();
            if (fileList != null && fileList.length > 0) {
                for (File value : fileList) {
                    // 如果下面还有文件
                    if (value.isDirectory()) {
                        size = size + getFolderSize(value);
                    } else {
                        size = size + value.length();
                    }
                }
            }
        }
        return size;
    }

    /**
     * 将文件大小显示为GB,MB等形式
     */
    public static String size(long size) {
        if (size / (1024 * 1024 * 1024) > 0) {
            float tmpSize = (float) (size) / (float) (1024 * 1024 * 1024);
            DecimalFormat df = new DecimalFormat("#.##");
            return "" + df.format(tmpSize) + "GB";
        } else if (size / (1024 * 1024) > 0) {
            float tmpSize = (float) (size) / (float) (1024 * 1024);
            DecimalFormat df = new DecimalFormat("#.##");
            return "" + df.format(tmpSize) + "MB";
        } else if (size / 1024 > 0) {
            return "" + (size / (1024)) + "KB";
        } else
            return "" + size + "B";

    }

    /**
     * 用于转换文件单位
     *
     * @param size 以Byte为单位的文件大小
     * @return FileSize 包含了文件转换后的大小及单位
     */
    @SuppressLint("DefaultLocale")
    public static String formatFileSize(long size) {
        float result = size;
        String unit = "B";
        if (result > 900) {
            unit = "KB";
            result = result / 1024;
        }
        if (result > 900) {
            unit = "MB";
            result = result / 1024;
        }
        if (result > 900) {
            unit = "GB";
            result = result / 1024;
        }
        DecimalFormat df = new DecimalFormat("#.##");
        return "" + df.format(result) + unit;
    }

    public static String getProcessInfo(Context context) {
        Process psProcess = null;
        try {
            psProcess = Runtime.getRuntime().exec("sh");
            DataOutputStream out = new DataOutputStream(psProcess.getOutputStream());
            InputStream is = psProcess.getInputStream();
            out.writeBytes("ps -t | grep;\n");
            out.writeBytes("ps\n");
            out.flush();
//            psProcess.waitFor();
            if (is.read() != 0) {
                byte firstByte = (byte) is.read();
                int available = is.available();
                byte[] characters = new byte[available + 1];
                characters[0] = firstByte;
                is.read(characters, 1, available);
                String re = new String(characters);
                Log.d("ddd", "process : " + re);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        ActivityManager.MemoryInfo info = new ActivityManager.MemoryInfo();
        activityManager.getMemoryInfo(info);
        Log.i("ddd", "系统剩余内存:" + size(info.availMem));
        Log.i("ddd", "系统已使用内存:" + size(info.totalMem - info.availMem));
        Log.i("ddd", "系统总内存:" + size(info.totalMem));
        Log.i("ddd", "系统是否处于低内存运行：" + info.lowMemory);
        Log.i("ddd", "当系统剩余内存低于" + size(info.threshold) + "MB时就看成低内存运行");

        return null;
    }

}
